package com.xhwl.centiotdata.handler.eesd.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.eesd.EesdFactory;
import com.xhwl.centiotdata.handler.eesd.EesdHandler;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.service.cent.IBiEnvCentService;
import com.xhwl.centiotdata.suppliersystem.eesd.en168.CsgHandler;
import com.xhwl.common.dto.cent.eesd.EDeviceOnlineDTO;
import com.xhwl.common.dto.cent.eesd.OdsEnvironmentMetricsDTO;
import com.xhwl.common.pojo.cent.bi.DwdFactEnvironmentMetrics;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/06/ 11:31
 */
@Slf4j
@Service
public class EesdCsgHandler extends EesdHandler {

    @Resource
    private CsgHandler csgHandler;
    @Resource
    private IBiEnvCentService biEnvCentService;

    /**
     * 11：南方电网-e能管家
     */
    @Override
    public void afterPropertiesSet() {
        EesdFactory.register(EesdMethodEnum.CSG.getId(), this);
    }

    /**
     * 电房同步原始数据/电房设备同步在离线数据
     * @param config
     */
    @Override
    public void pullEnvData(DeviceProjSupplierCategory config) {
        try {
            List<OdsEnvironmentMetricsDTO> odsEnvironmentMetricsDTOList = csgHandler.getERoomEnv(config);
            String dataStr = JSONObject.toJSONString(odsEnvironmentMetricsDTOList);

            String realTimeDataStr = JSONObject.toJSONString(JSONObject.parseArray(dataStr, DwdFactEnvironmentMetrics.class));
            log.info("环境同步电房实时数据 开始： {}", realTimeDataStr);
            biEnvCentService.pushEnvRealTimeData(realTimeDataStr);
            log.info("环境同步电房实时数据 结束");

            List<EDeviceOnlineDTO> eRoomDeviceOnline = csgHandler.getERoomDeviceOnline(config);
            String statusDataStr = JSONObject.toJSONString(eRoomDeviceOnline);
            log.info("电房设备状态 开始： {}", statusDataStr);
            biEnvCentService.asyncERoomDeviceOnline(statusDataStr);
            log.info("电房设备状态 结束");
        } catch (Exception e) {
            log.error("电房原始数据 失败： {}",  e.getMessage());
        }
    }

    /**
     * 电房环境同步历史数据
     * @param config
     */
    @Override
    public void pullEnvHourData(DeviceProjSupplierCategory config) {
        try {
            List<OdsEnvironmentMetricsDTO> odsEnvironmentMetricsDTOList = csgHandler.getERoomEnv(config);
            String dataStr = JSONObject.toJSONString(odsEnvironmentMetricsDTOList);
            //log.info("电房历史数据 开始： {}", dataStr);
            biEnvCentService.asyncEnvHourData(dataStr);
            log.info("电房历史数据 结束");
        } catch (Exception e) {
            log.error("电房历史数据 失败： {}",  e.getMessage());
        }
    }

}
